$(document).ready(function() {
    //var url = window.location.protocol + "//" + window.location.host + '/portfolio/newidu/';
    var url = window.location.protocol + "//" + window.location.host + '/';
    $('.checkCompare').click(function() {
        var totalChecked = $('#carCompare').find(':checked').length;


        if ($(this).is(":checked")) {
            $(this).parent().find('a').addClass('selected');
        } else {
            $(this).parent().find('a').removeClass('selected');
        }

        switch (totalChecked)
        {
            case 1:
                $('#carCompareSubmit').attr('disabled', true);
                break;
            case 2:
                $('#carCompareSubmit').attr('disabled', false);
                break;
            case 3:
                //alert('Puede compara un maximo de 4 vehiculos!');
                $('#carCompare').find('input:checkbox:not(:checked)').each(function() {
                    $(this).attr('disabled', false);
                });
                break;
            case 4:
                $('#carCompare').find('input:checkbox:not(:checked)').each(function() {
                    $(this).attr('disabled', true);
                });
                //$('.checkCompare').attr('disabled', true);
                break;
        }

    });
    $('#carCompare').submit(function() {
        /*$(this).find(':checkbox:checked').each(function() {
         alert($(this).val());
         });*/
        $('#compare-results').html('');
        var dataString = $(this).serialize();
        console.log(dataString);
        $.ajax({
            url: url + 'site/ajax_compare_call',
            type: 'POST',
            dataType: 'json',
            data: dataString,
            cache: false,
            beforeSend: function() {
                //$('#loader').show();
                //$('#results').html('');
            },
            success: function(output_string) {
                $('#compare-results').html(output_string);
            }
        });
        return false;
    });
    $('#resetButton').click(function() {
        $('#carCompareSubmit').attr('disabled', true);
        $('.checkCompare').attr('disabled', false);
        $('.checkCompare').find('a.selected').removeClass('selected');
    });
});


